import java.awt.Color;
import java.awt.Component;
import java.awt.DisplayMode;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
import quicktime.QTException;
import quicktime.QTSession;
import quicktime.app.view.MoviePlayer;
import quicktime.app.view.QTFactory;
import quicktime.io.OpenMovieFile;
import quicktime.io.QTFile;
import quicktime.std.StdQTException;
import quicktime.std.movies.Movie;

class ScreenElement {

	Screen screen;

	JFrame videoFrame;

	MoviePlayer qtPlayer;

	private Movie qtMovie;

	private Component qtCanvas;

	int w = 0;

	int center_x;

	int center_y;

	int center_z;

	int origin_x;

	int origin_y;

	int origin_z;

	int end_x;

	int end_y;

	int end_z;

	float z = 0;

	float y = 0;

	float x = 0;

	float xr = 0.0f;

	float yr = 0.0f;

	float zr = 0.0f;

	float vx = 0;

	float vy = 0;

	float vz = 0;

	float vxr = 0;

	float vyr = 0;

	float vzr = 0;

	float va = 0;

	float alpha = 0;

	float startAlpha = 0;

	float endAlpha = 240;

	int myColor;

	String mediaFileName = null;

	PImage pimg;

	String theString;

	int depth = -1000;

	int videoDuration = 0;

	int midddleOffset;

	PFont myFont;

	Random myRandom = new Random();

	int red, green, blue;

	boolean inPreview;

	int start_y = 0;

	public ScreenElement(Screen _s, String _st, PFont _font, int _x, int _y, int _color) {
		screen = _s;
		center_x = _x;
		start_y = _y;
		// center_y = _y + screen.getTopSpacing();

		theString = _st;
		// movie = null;

		pimg = null;
		myFont = _font;
		midddleOffset = DisplayApp.contentMetrics.stringWidth(_st) / 2;
		red = _color & 0xFF0000;
		red = red >> 16;
		green = _color & 0x00FF00;
		green = green >> 8;
		blue = _color & 0x0000FF;
		// System.out.println("x" + _x + " y" + _y);
		setEndPoints();

	}

	/*
	 * public ScreenElement( Screen _s, QTMoviePixelSource _m,int _x, int _y){
	 * center_x = _x; center_y = _y; screen = _s; theString = null; movie = _m;
	 * pimg = null; setEndPoints(); }
	 */

	public ScreenElement(Screen _s, String _mediaFileName, int _x, int _y) {
		screen = _s;
		center_x = _x;
		center_y = _y ; //+ screen.getTopSpacing();
		start_y = _y;
		theString = null;
		mediaFileName = _mediaFileName;
		// System.out.println("mediaFileName" + _mediaFileName);
		if (_mediaFileName.endsWith(".mov") || _mediaFileName.endsWith(".mpg")) {
			try {
				QTSession.open();

				System.out.println("QTSession Open");

			} catch (QTException qte) {
				System.out.println("Sorry NOT: QTSession Open");
			}
			// open it just to get the duration.
			try {
				OpenMovieFile omf = OpenMovieFile.asRead(new QTFile(mediaFileName));
				qtMovie = Movie.fromFile(omf);

				// mediaFileName = _mediaFileName;
			} catch (Exception e) {
				// TODO: handle exception
			}
			if (qtMovie != null) {
				try {
					System.out.println("movie time" + qtMovie.getTimeScale());
					videoDuration = 1000 * qtMovie.getDuration() / qtMovie.getTimeScale();
				} catch (StdQTException e) {
					e.printStackTrace();
					videoDuration = 3000;
					// TODO Auto-generated catch block

				}
			} else {
				videoDuration = 3000;
			}

			qtMovie = null;
		} else {

		}
		setEndPoints();
	}

	public void unload() {
		pimg = null;
		qtMovie = null;
		qtPlayer = null;
		qtCanvas = null;
		inPreview = true;
	}

	public void load() {

		if ((mediaFileName != null) && qtMovie == null && (mediaFileName.endsWith(".mov") || mediaFileName.endsWith(".mpg"))) {
			try {
				OpenMovieFile omf = OpenMovieFile.asRead(new QTFile(mediaFileName));
				qtMovie = Movie.fromFile(omf);
			} catch (Exception e) {
				// TODO: handle exception
			}

			try {

				qtPlayer = new MoviePlayer(qtMovie);
				qtCanvas = (QTFactory.makeQTComponent(qtMovie)).asComponent();

			} catch (Exception e) {
				System.out.println("Trouble Making Movie");
			}
			videoFrame = new JFrame();
			videoFrame.setBackground(Color.BLACK);

			try {
				videoFrame.setSize(qtMovie.getNaturalBoundsRect().getWidth(), qtMovie.getNaturalBoundsRect().getHeight());
			} catch (StdQTException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			videoFrame.getContentPane().add(qtCanvas);
			videoFrame.setUndecorated(true);
			videoFrame.setLocation((DisplayApp.width - videoFrame.getWidth()) / 2, (DisplayApp.height - videoFrame.getHeight()) / 2);

		} else if (mediaFileName != null && pimg == null && (mediaFileName.endsWith(".jpg") || mediaFileName.endsWith(".png"))) {
			// System.out.println("GetReady" + imageFileName);
		//	BufferedImage img = null;
			
			//try {
				if (DisplayApp.isApplication) {
					//img = ImageIO.read(new File(mediaFileName));
					pimg = DisplayApp.myApplet.loadImage(mediaFileName);
				} else {
					pimg = DisplayApp.myApplet.loadImage(mediaFileName);
					Screen.resize( pimg, DisplayApp.height - screen.topSpacing);
					//img = ImageIO.read(new URL(mediaFileName));
					
					//img = Screen.resize(img, DisplayApp.height - screen.topSpacing);
				}
			//} catch (IOException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
			//}
			/*
			try {
				pimg = new PImage(img);
			} catch (Exception e) {
				System.out.println("Problem turning buffered image to pimage " + e);
			}
			*/

		}

	}

	/*
	 * public void setElementCenterOffset(int _mx, int _my, int _mz) {
	 * 
	 * center_x += _mx; center_y += _my; center_z += _mz; }
	 */
	public void setElementEndPoints(int _ox, int _oy, int _oz, int _dx, int _dy, int _dz, boolean _addrandom) {
		origin_x = _ox;
		origin_y = _oy;
		origin_z = _oz;

		end_x = _dx;
		end_y = _dy;
		end_z = _dz;
	}

	public void setEndPoints() {
		// myFont = DisplayApp.contentFont;
		center_z = 0;

		// myRandom.setSeed(System.currentTimeMillis());
		if (mediaFileName != null) {
			// System.out.println("setEndPoints" + mediaFileName);
			origin_x = center_x; // DisplayApp.width + myRandom.nextInt(3 *
									// DisplayApp.width);
			origin_y = center_y; // myRandom.nextInt(3 * DisplayApp.height);
			origin_z = 0; // depth;
			// origin_z = zDepth;
			end_z = 0; // depth;
			end_x = center_x; // -myRandom.nextInt(3 * DisplayApp.width);
			end_y = center_y; // -myRandom.nextInt(3 * DisplayApp.height);
		} else {
			origin_x = DisplayApp.width + myRandom.nextInt(3 * DisplayApp.width);
			origin_y = myRandom.nextInt(3 * DisplayApp.height);
			origin_z = depth;
			// origin_z = zDepth;
			end_z = depth;
			end_x = -myRandom.nextInt(3 * DisplayApp.width);
			end_y = -myRandom.nextInt(3 * DisplayApp.height);
		}
	}

	public void startElement() {

		// end_x = 0;
		// end_y = 0;
		// System.out.println(inPreview + " Start " + theString);

		center_y = start_y + screen.getTopSpacing();
		if (inPreview == false) {
			x = origin_x;
			y = origin_y;
			z = origin_z;
			if (pimg == null) {
				xr = myRandom.nextFloat() * 6.24f;
				yr = myRandom.nextFloat() * 6.24f;
				zr = myRandom.nextFloat() * 6.24f;
			} else {
				xr = 0.0f;
				yr = 0.0f;
				zr = 0.0f;
			}

		} else {
			alpha = 200;

		}
		inPreview = false;
		/*
		 * if (origins == null){ x = origin_x; y = origin_y; z = origin_z;
		 * }else{ if (x == 0) x = origin_x; else x = origins[0]; if (y == 0) y =
		 * origin_y; else y = origins[1]; if (z == 0) z = origin_z; else z =
		 * origins[2]; }
		 */

		// double r = myRandom.nextDouble()* 2*Math.PI;

		// System.out.println(theString + "xr" + xr + " x"+ x + " random" +
		// myRandom.nextInt(7));
		if (pimg == null) {
			setVectors(center_x, center_y, center_z, 2 * Math.PI, 2 * Math.PI, 2 * Math.PI);

		} else {
			setVectors(center_x, center_y, center_z, 0, 0, 0);
		}
		// flipEndPoints();
		alpha = startAlpha;
		va = (endAlpha - startAlpha) / screen.numOfTransitionframes;

	}

	public void setFont(PFont _f) {
		myFont = _f;
	}

	public int getVideoDuration() {
		return videoDuration;
	}

	public void startOut() {
		// System.out.println("Arrived" + qtMovie );
		// System.out.println(theString + "ex" + endx + " ey"+ ey + " random" +
		// myRandom.nextInt(7));
		if (pimg == null) {
			// setVectors(center_x, center_y, center_z, 2 * Math.PI, 2 *
			// Math.PI, 2 * Math.PI);
			setVectors(end_x, end_y, end_z, myRandom.nextFloat() * 6.24f, myRandom.nextFloat() * 6.24f, myRandom.nextFloat() * 6.24f);

		} else {
			setVectors(end_x, end_y, end_z, 0, 0, 0);

			// setVectors(center_x, center_y, center_z, 0, 0, 0);
		}
		va = -va;
		if (qtMovie != null) {
			try {
				qtMovie.stop();
			} catch (StdQTException e) {
				System.out.println("Can't Start Movie");
			}
			videoFrame.setVisible(false);
			videoFrame.setVisible(true);
			DisplayApp.setFullScreen();
			DisplayApp.myApplet.setVisible(true);

			videoFrame.dispose();

			// DisplayApp.myApplet.remove(qtCanvas);

		}
	}

	public void arrived() {
		// System.out.println("Arrived" + qtMovie );
		if (qtMovie != null) {
			DisplayApp.myApplet.setVisible(false);

			try {
				Thread.sleep(10);
			} catch (InterruptedException e1) {
			}
			int vidW = DisplayApp.myApplet.width;
			int vidH = DisplayApp.myApplet.height;

			try {
				vidW = qtMovie.getBounds().getWidth();
				vidH = qtMovie.getBounds().getHeight();
				qtCanvas.setSize(qtMovie.getBounds().getWidth(), qtMovie.getBounds().getHeight());
				qtCanvas.setLocation((DisplayApp.width - vidW) / 2, (DisplayApp.height - vidH) / 2);
				qtMovie.setTimeValue(0);
				qtMovie.start();

				// qtPlayer.setRate(1);
			} catch (StdQTException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			videoFrame.setVisible(true);
			DisplayApp.setFullScreen(null, videoFrame, vidW, vidH);
			// Graphics frameg = videoFrame.getGraphics();
			// frameg.clearRect(0, 0, videoFrame.getWidth(),
			// videoFrame.getHeight());

			// videoFrame.pack();

			// try {

			// videoFrame.pack();

		}

	}

	void setVectors(int _destination_x, int _destination_y, int _destination_z, double _destination_xr, double _destination_yr, double _destination_zr) {
		vx = (_destination_x - x) / screen.numOfTransitionframes;
		vy = (_destination_y - y) / screen.numOfTransitionframes;
		vz = (_destination_z - z) / screen.numOfTransitionframes;
		vxr = (float) ((_destination_xr - xr) / screen.numOfTransitionframes);
		vyr = (float) ((_destination_yr - yr) / screen.numOfTransitionframes);
		vzr = (float) ((_destination_zr - zr) / screen.numOfTransitionframes);
	}

	void incrementPosition() {
		x = x + vx;
		y = y + vy;
		z = z + vz;
		xr = xr + vxr;
		yr = yr + vyr;
		zr = zr + vzr;
		alpha = alpha + va;
	}

	public void setPreview(boolean _inPreview) {
		inPreview = _inPreview;
	}

	void preview(PApplet _pApplet, int _x, int _y, float _angle) {

		if (inPreview == false)
			return;

		// z = -10;
		z = -700;
		x = PApplet.lerp(x, _x, .18f);
		// x = _x;
		y = PApplet.lerp(y, _y, .18f);
		// y = _y;
		zr = PApplet.lerp(zr, _angle, .02f);

		if (theString != null) {
			_pApplet.pushMatrix();

			_pApplet.translate(x + midddleOffset, y, z);
			_pApplet.translate(-midddleOffset, 0, 0);
			_pApplet.rotateX(xr);
			_pApplet.rotateY(yr);
			_pApplet.rotateZ(zr);

			// _pApplet.scale(.4f);
			// _pApplet.fill(red, green, blue, alpha);
			_pApplet.fill(0, 127);
			_pApplet.textFont(myFont);
			_pApplet.text(theString, 0, 0);
			// System.out.println("preview" + x + " " + y + " " + z + " " + zr +
			// theString);
			_pApplet.popMatrix();
		} else if (pimg != null) {
			_pApplet.pushMatrix();

			_pApplet.translate(x, y, z);
			// _pApplet.scale(.1f);
			// _pApplet.rotateX(xr);
			// _pApplet.rotateY(yr);
			_pApplet.rotateZ(zr);
			try {
				_pApplet.tint(255, 255, 255, 100);
				_pApplet.image(pimg, 0, 0);
			} catch (Exception e) {
				System.out.println("Problem drawing image" + e);
			}
			// System.out.println("preview img" + x);
			_pApplet.popMatrix();

		} else if (qtMovie != null) {
			// DisplayApp.myApplet.add(qtCanvas);
			// videoFrame.repaint();

		}
	}

	void display(PApplet _pApplet) {
		// if (true) return;

		if (theString != null) {
			_pApplet.pushMatrix();
			_pApplet.translate(x + midddleOffset, y, z);
			_pApplet.translate(-midddleOffset, 0, 0);
			// _pApplet.translate(x, y , z);
			_pApplet.rotateX(xr);
			_pApplet.rotateY(yr);
			_pApplet.rotateZ(zr);
			_pApplet.fill(red, green, blue, alpha);
			_pApplet.textFont(myFont);
			_pApplet.text(theString, 0, 0);
			_pApplet.popMatrix();
		} else if (pimg != null) {
			_pApplet.pushMatrix();

			_pApplet.translate(x, y, z);
			_pApplet.rotateX(xr);
			_pApplet.rotateY(yr);
			_pApplet.rotateZ(zr);
			try {
				_pApplet.tint(255, 255, 255, alpha);
				_pApplet.image(pimg, 0, 0);
			} catch (Exception e) {
				System.out.println("Problem drawing image" + e);
			}

			_pApplet.popMatrix();

		} else if (qtMovie != null) {
			// DisplayApp.myApplet.add(qtCanvas);
			// videoFrame.repaint();
			// System.out.println("movie" + qtMovie);

		}

	}

}
